package com.numbfish.ch04_ex32_mybatis_plus_mysql;

import com.baomidou.mybatisplus.core.batch.MybatisBatch;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.numbfish.ch04_ex32_mybatis_plus_mysql.entity.SysUser;
import com.numbfish.ch04_ex32_mybatis_plus_mysql.mapper.SysUserMapper;
import com.numbfish.ch04_ex32_mybatis_plus_mysql.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
@Slf4j

public class Ch04Ex32MybatisPlusMysqlTestApp {
    @Autowired
    private  ApplicationContext applicationContext;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;


    @Test
    void contextLoads() {
        log.info("applicationContext = {}",applicationContext);
    }
    @Test
    void beanCount(){
        MyMetaObjectHandler bean = applicationContext.getBean(MyMetaObjectHandler.class);
        Assertions.assertNotNull(bean);
    }


    void outputTheBeanFromContext(Class<?> clazz){
        // 用户未定义任何类时的bean个数为152
        log.info("applicationContext get a bean  {} === {}",clazz,applicationContext.getBean(clazz));
    }

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<SysUser> userList = userMapper.selectList(null);
        Assertions.assertTrue(userList.size()>0);
        userList.forEach(System.out::println);
    }

    @Test
    public void testInsert() {
        System.out.println(("----- insert method test ------"));

        SysUser sysUser = new SysUser();
        sysUser.setId(19L);
        sysUser.setName("刘邦");
        sysUser.setAge(30);
        sysUser.setEmail("xiangyu@134.com");
        sysUser.setDeleted(0);

        userMapper.insert(sysUser);
    }

    @Test
    public void testUpdate() {
        System.out.println(("----- insert method test ------"));

        SysUser sysUser = new SysUser();
        sysUser.setId(19L);
        sysUser.setName("刘邦高祖");
        sysUser.setAge(30);
        sysUser.setEmail("xiangyu@134.com");
        sysUser.setDeleted(0);

        userMapper.updateById(sysUser);
    }

    // 此方法并未将数据插入数据库
    @Test
    public void testBatch(){
        ArrayList<SysUser> userList = new ArrayList<>();
        for (int i = 20; i < 23; i++) {
            SysUser sysUser = new SysUser();
            sysUser.setId(Long.valueOf(i));
            sysUser.setName("批量"+i);
            sysUser.setAge(i);
            sysUser.setDeleted(0);
            userList.add(sysUser);
        }
        iUserService.saveBatch(userList);
    }

    @Test
    public void testBatch2(){
        ArrayList<SysUser> userList = new ArrayList<>();
        for (int i = 23; i < 25; i++) {
            SysUser sysUser = new SysUser();
            sysUser.setId(Long.valueOf(i));
            sysUser.setName("批量"+i);
            sysUser.setAge(i);
            // sysUser.setDeleted(0);
            userList.add(sysUser);
        }
        MybatisBatch<SysUser> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, userList);
        MybatisBatch.Method<SysUser> method = new MybatisBatch.Method<>(SysUserMapper.class);
        mybatisBatch.execute(method.insert());
    }

    @Test
    public void testPage(){
        Page<SysUser> page = new Page<>(3, 5);
        IPage<SysUser> userIPage = userMapper.selectPage(page, new QueryWrapper<SysUser>().eq("age", 20));
        long pages = userIPage.getPages();
        long current = userIPage.getCurrent();
        //List<SysUser> records = userIPage.getRecords();
        long size = userIPage.getSize();
        long total = userIPage.getTotal();
        System.out.println("pages="+pages);
        System.out.println("current="+current);
        System.out.println("size="+size);
        System.out.println("total="+total);
    }
}